package com.foryou.wechat.service.impl;

import com.foryou.wechat.common.enums.CapacityEnum;
import com.foryou.wechat.common.enums.InviteTypeEnum;
import com.foryou.wechat.entity.InviteInfoEntity;
import com.foryou.wechat.entity.ProductionEntity;
import com.foryou.wechat.entity.ShootEntity;
import com.foryou.wechat.mapper.InviteInfoMapper;
import com.foryou.wechat.mapper.ProductionMapper;
import com.foryou.wechat.mapper.ShootMapper;
import com.foryou.wechat.model.dto.InviteInfoDTO;
import com.foryou.wechat.model.vo.InviteInfoVo;
import com.foryou.wechat.service.InviteInfoService;
import com.foryou.wechat.utils.EnumUtil;
import com.foryou.wechat.utils.HttpContextUtils;
import com.foryou.wechat.utils.RelativeDateFormat;
import com.foryou.wechat.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Lee.
 */
@Service
public class InviteInfoServiceImpl implements InviteInfoService {

    private final InviteInfoMapper inviteInfoMapper;

    private final ShootMapper shootMapper;

    private final ProductionMapper productionMapper;

    public InviteInfoServiceImpl(InviteInfoMapper inviteInfoMapper, ShootMapper shootMapper, ProductionMapper productionMapper) {
        this.inviteInfoMapper = inviteInfoMapper;
        this.shootMapper = shootMapper;
        this.productionMapper = productionMapper;
    }

    @Override
    public int addWorksInviteInfo(InviteInfoDTO inviteInfoDTO) {
        return addInviteInfo(inviteInfoDTO,InviteTypeEnum.WORKS);
    }

    @Override
    public int addShootInviteInfo(InviteInfoDTO inviteInfoDTO) {
        return addInviteInfo(inviteInfoDTO,InviteTypeEnum.SHOOT);
    }

    @Override
    public Response getInviteInfoDetail(Long inviteInfoId) {

        InviteInfoVo inviteInfoVo = inviteInfoMapper.getInviteInfoVo(inviteInfoId);
        String capacity = EnumUtil.getEnumByCode(CapacityEnum.class, Integer.parseInt(inviteInfoVo.getCapacity()));
        inviteInfoVo.setCapacity(capacity);

        //执行已读修改
        InviteInfoEntity infoEntity = inviteInfoMapper.selectById(inviteInfoId);
        if (infoEntity.getUnRead() == 0) {
            infoEntity.setUnRead(1);
            inviteInfoMapper.updateById(infoEntity);
        }

        return Response.success(inviteInfoVo);
    }

    @Override
    public Response findInviteListForReceiveUser(int start, Long userId) {
        List<InviteInfoVo> receiveInviteList = inviteInfoMapper.findReceiveInviteList(start, userId);
        receiveInviteList.forEach(e->{
            e.setCapacity(
                    EnumUtil.getEnumByCode(CapacityEnum.class, Integer.valueOf(e.getCapacity()))
            );
        });
        return Response.success(receiveInviteList);
    }

    @Override
    public Response findInviteListForSendUser(int start, Long userId) {
        List<InviteInfoVo> receiveInviteList = inviteInfoMapper.findSendInviteList(start, userId);
        receiveInviteList.forEach(e->{
            e.setCapacity(
                    EnumUtil.getEnumByCode(CapacityEnum.class, Integer.valueOf(e.getCapacity()))
            );
            e.setTime(RelativeDateFormat.format(e.getCreateTime()));
        });
        return Response.success(receiveInviteList);
    }

    @Transactional
    public int addInviteInfo(InviteInfoDTO inviteInfoDTO, InviteTypeEnum inviteTypeEnum) {
        Long userId = HttpContextUtils.getUserId();
        //得到介绍信息
        final String intro;
        switch (inviteTypeEnum) {
            case SHOOT:
                ShootEntity shootEntity = shootMapper.selectById(inviteInfoDTO.getBusinessId());
                intro = shootEntity.getRequirement();
                break;
            case WORKS:
                ProductionEntity productionEntity = productionMapper.selectById(inviteInfoDTO.getBusinessId());
                intro = productionEntity.getIntro();
                break;
            default:
                intro = "";
                break;
        }

        return inviteInfoMapper.insert(new InviteInfoEntity(
                inviteInfoDTO.getBusinessId(),
                userId,
                inviteInfoDTO.getToUserId(),
                inviteInfoDTO.getPhone(),
                inviteInfoDTO.getWeChat(),
                inviteInfoDTO.getWeibo(),
                inviteInfoDTO.getReason(),
                intro,
                inviteTypeEnum.getCode()
        ));
    }
}
